package com.sky.controller;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 订单相关接口
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    
    @Autowired
    private OrderService orderService;
    
    /**
     * 支付成功回调处理
     * @param outTradeNo
     */
    @PutMapping("/paySuccess")
    public void paySuccess(@RequestParam String outTradeNo) {
        orderService.paySuccess(outTradeNo);
    }
    
    /**
     * 根据状态和下单时间查询订单
     * @param status
     * @param time
     * @return
     */
    @GetMapping("/getByStatusAndOrdertimeLT")
    public List<Orders> getByStatusAndOrdertimeLT(@RequestParam Integer status, @RequestParam LocalDateTime time) {
        return orderService.getByStatusAndOrdertimeLT(status, time);
    }
    
    /**
     * 更新订单信息
     * @param orders
     */
    @PutMapping("/update")
    public void update(@RequestBody Orders orders) {
        orderService.update(orders);
    }
    
    /**
     * 根据状态和时间范围统计订单数量
     * @param map
     * @return
     */
    @PostMapping("/countByMap")
    public Integer countByMap(@RequestBody Map map) {
        return orderService.countByMap(map);
    }
    
    /**
     * 根据时间范围查询营业额
     * @param map
     * @return
     */
    @PostMapping("/sumByMap")
    public Double sumByMap(@RequestBody Map map) {
        return orderService.sumByMap(map);
    }
    
    /**
     * 查询商品销量排名
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/getSalesTop10")
    public List<GoodsSalesDTO> getSalesTop10(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                                             @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        return orderService.getSalesTop10(beginTime, endTime);
    }
}